package ar.edu.unicen.pladema.vc1.practico2;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;

import ar.edu.unicen.pladema.ui.model.ColorRange;
import ar.edu.unicen.pladema.vc1.fractal.Fractal;
import ar.edu.unicen.pladema.vc1.model.GraphicAttributes;
import ar.edu.unicen.pladema.vc1.practico.PracticoDrawer;

public class P2Enunciado2Drawer extends PracticoDrawer {
	private Fractal fractal;
	private static ColorRange cr=null;
	
	public P2Enunciado2Drawer(GraphicAttributes ga) {
		super(ga);
		this.fractal = new Fractal();
	}

	public ColorRange testPaleta() {
		ColorRange testCR = new ColorRange(0, new RGB(0,0,0));
//		testCR.add(32, new RGB(255,0,255));
		//cr.add(130, new RGB(255,0,0));
		testCR.add(64, new RGB(255,255,0));
//		testCR.add(96, new RGB(0,255,0));
		testCR.add(128, new RGB(0,255,255));
//		testCR.add(160, new RGB(0,0,255));
		testCR.add(192, new RGB(0,25,255));
//		testCR.add(224, new RGB(25,25,255));
		testCR.add(255, new RGB(255,255,255));
		return testCR;
	}
	@Override
	protected void paintControlImpl(PaintEvent e) {
		if (cr == null) {
			cr = testPaleta();
		}

		fractal.setZoom(graphicAttributes.getDouble(GraphicAttributes.ZOOM));
		int moveX = graphicAttributes.getInt(GraphicAttributes.OFFSET_X);
		int moveY = graphicAttributes.getInt(GraphicAttributes.OFFSET_Y);

		//Double diagonal = Math.sqrt((width*width)+(height*height));
		
		for (int i=0; i<e.width; i++ ) {
			for (int j=0; j<e.height; j++) {
				int c = fractal.doCount(i+moveX, j+moveY);
				
				Color color = null;
				if (c == -1 ) {
					color = new Color (e.gc.getDevice(), 0, 255, 0);
				} else {
					color = new Color (e.gc.getDevice(), cr.get(c));
				}
				e.gc.setForeground(color);
				e.gc.drawPoint(i, j);
			}
		}
	}
}
